<?php
class GoodsAction extends CommonAction {
   

	public function __construct(){
		parent::__construct();
		$this->checkAuth();
		$this->checkReal();

	}
	//空操作
	Public function _empty(){
	   header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码
        $this->display("Public:404");
	}
	public function index(){
		$where['status']=1;
		if(!empty($_GET['cat_id'])){
			$this->assign("cat_id",$_GET['cat_id']);
			$where['cat_id']=$_GET['cat_id'];
		}else{
			$this->assign("cat_id",aaa);
		}
		$Goods = M('Goods'); // 实例化User对象
		import('ORG.Util.Page');// 导入分页类
		$count      = $Goods->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,16);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $Goods->where($where)->limit($Page->firstRow.','.$Page->listRows)->limit(4)->select();
		
		foreach ($list as $k=>$v){
			
			$list[$k]['count_all']=intval(M("Goods_user")->where("goods_id={$v['id']} and status = 1")->sum("num"));
			$list[$k]['shengyu_num']=$v['num']-$list[$k]['count_all'];
		}
		
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		
		$category=M("Goods_category")->select();
		$this->assign("category",$category);
		
		$hot=$Goods->where("status = 1 and position =1")->select();
		$this->assign("hot",$hot);
		
	    $this->display('./Tpl/Home/Goods_index.html');
	}

	public function view(){
		$id=intval($_GET['id']);
		$goods=M("Goods")->where("id='$id'")->find();
		
		$hot=M("Goods")->where("status = 1 and position =1")->limit(4)->select();
		$this->assign("hot",$hot);
		$this->assign("goods",$goods);
		$this->assign("one",1);
		$count=M("Goods_user")->where("goods_id='$id' and status = 1")->sum("num");
		$this->assign("count",$count);
		$this->display('./Tpl/Home/Goods_view.html');
	}
	
	public function add_goods(){

		if(empty($_POST['num'])){
			$arr['status']=2;
			$arr['info']="请输入数量";
			$this->ajaxReturn($arr);
		}
		if(empty($_POST['tel'])){
			$arr['status']=3;
			$arr['info']="请输入电话号码";
			$this->ajaxReturn($arr);
		}
		if(empty($_POST['address'])){
			$arr['status']=4;
			$arr['info']="请输入地址";
			$this->ajaxReturn($arr);
		}
		
		$goodid=$_POST['goodsid'];
		$goods=M("Goods")->where("id='$goodid'")->find();
		
		if(empty($goods)||$goods['status']!=1){
			$arr['status']=5;
			$arr['info']="无效商品无法购买";
			$this->ajaxReturn($arr);
		}
		$goodsuser_count=M("Goods_user")->where("goods_id='$goodid' and status = 1")->sum("num");
		if($goodsuser_count+$_POST['num']>$goods['num']){//判断数量
			$arr['status']=8;
			$arr['info']="库存不足，无法购买";
			$this->ajaxReturn($arr);
		}
		
		
		//计算价格
		$rmb=$_POST['num']*$goods['money_new']*($goods['rmb_bili']/100);			
		$xnb=$_POST['num']*$goods['money_new']*(1-$goods['rmb_bili']/100);
		$user=M("User")->where("id='{$_SESSION['USER_KEY_ID']}'")->find();
		if($user['rmb']<$rmb || $user['xnb']<$xnb){
			$arr['status']=6;
			$arr['info']="您的金额不足，无法购买";
			$this->ajaxReturn($arr);
		}
		
		
		
		$data['uid']=$_SESSION['USER_KEY_ID'];
		$data['goods_id']=$goodid;
		$data['tel']=$_POST['tel'];
		$data['address']=$_POST['address'];
		$data['num']=$_POST['num'];
		$data['rmb_money']=$rmb;
		$data['xnb_money']=$xnb;
		$data['status']=0;
		$data['add_time']=time();
		
		$re=M("Goods_user")->add($data);
		if($re){
			M("User")->where("id='{$_SESSION['USER_KEY_ID']}'")->setDec("xnb",$xnb);
			M("User")->where("id='{$_SESSION['USER_KEY_ID']}'")->setDec("rmb",$rmb);
			$arr['status']=1;
			$arr['info']="购买成功";
			$this->ajaxReturn($arr);
		}else{
			$arr['status']=7;
			$arr['info']="购买失败";
			$this->ajaxReturn($arr);
		}
		
	}
	
	
	
	public function goumai_list(){
		$GU = M('Goods_user'); // 实例化User对象
		$where['uid']=$_SESSION['USER_KEY_ID'];
		import('ORG.Util.Page');// 导入分页类
		$count      = $GU->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$field="t_goods_user.*,t_goods.pic,t_goods.title,t_goods.rmb_bili";
		$list = $GU->where($where)->field($field)->join("t_goods on t_goods_user.goods_id=t_goods.id")->order('t_goods.add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($list as $k=>$v){
			$list[$k]['status_gu']=$this->gu_status($v['status']);
		}
		
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display('./Tpl/Home/goumai_list.html');
	}
	
	
	/**
	 * 格式化购买状态
	 */
	private function gu_status($status){
		switch ($status){
			case -1:$name="审核未通过";break;
			case 0:$name="未审核";break;
			case 1:$name="审核通过";break;
			default:$name="未知状态";
		}
		return $name;
	}
	
	
	
	
	
}